/**
 * 
 */
package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;

import extend.org.compiere.utils.Util;


/**
 * @author V.Sokolov
 *
 */
public class MBPMSubs extends X_BPM_Subs {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -635594007776277367L;

	/**
	 * @param ctx
	 */
	public MBPMSubs(Properties ctx) {
		super(ctx);
	}

	/**
	 * @param ctx
	 * @param BPM_Subs_ID
	 * @param trxName
	 */
	public MBPMSubs(Properties ctx, int BPM_Subs_ID, String trxName) {
		super(ctx, BPM_Subs_ID, trxName);
	}

	/**
	 * @param ctx
	 * @param rs
	 * @param trxName
	 */
	public MBPMSubs(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
	}
	
	/*
	 * 
	 */
	@Override
	protected boolean beforeSave(boolean newRecord) {

		if(getBPM_Share() > 100)
			throw new AdempiereException(Msg.translate(getCtx(), "Share > 100"));
		
		int i = 0;
		if(!isHead()) i++;
		
		String errmsg = "";
		if(getCountHead()-i >= 1)
			errmsg = "Is Head > 1";
		Util.checkErrMsg(errmsg);
		
		return true;
	}
	
	public static int getSubsID(int AD_User_ID){
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int result = MBPMSubs.getHeadSubsID();
		
		String sql_ = "SELECT "+I_BPM_SubsLine.COLUMNNAME_BPM_Subs_ID+" FROM "+I_BPM_SubsLine.Table_Name+" \n" +
						"WHERE "+I_BPM_SubsLine.COLUMNNAME_AD_User_ID+"="+AD_User_ID;
		
		try
		{
			pstmt = DB.prepareStatement(sql_,null);
			rs = pstmt.executeQuery();
			if(rs.next())
				return rs.getInt(1);
		}
		catch (SQLException e)
		{
			CLogger.get().log(Level.INFO, "SQL = "+sql_, e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		
		return result;
	}
	
	public static int getHeadSubsID(){
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		
		String sql = "SELECT "+I_BPM_Subs.COLUMNNAME_BPM_Subs_ID+" FROM "+I_BPM_Subs.Table_Name+" \n" +
						"WHERE "+I_BPM_Subs.COLUMNNAME_isHead+"='Y'";
		try
		{
			pstmt = DB.prepareStatement(sql,null);
			rs = pstmt.executeQuery();	
					
			if(rs.next ())
				return rs.getInt(1);
		}
		catch (SQLException e)
		{
			CLogger.get().log(Level.INFO, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		
		return 0;
	}
	
	private int getCountHead(){
		
		List<MBPMSubs> list = new Query(Env.getCtx(), I_BPM_Subs.Table_Name, I_BPM_Subs.COLUMNNAME_isHead+"=?", null)
		.setParameters("Y")
		.setOnlyActiveRecords(true)
		.list();
		
		return list.size();
	}
	
	public static X_C_Project[] getLinesProject(Properties ctx, int BPM_Subs_ID, String trxName){
		
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ArrayList<X_C_Project> result = new ArrayList<X_C_Project>();
		
		String sql = "select r.* from ad_treenode t \n"
						+"join c_project r on r.c_project_id=t.node_id \n"
						+"where ad_tree_id=1000012 and parent_Id in (select c_project_id from c_project where bpm_Subs_id = "+BPM_Subs_ID+") \n"
						+"order by t.seqNo";
		try
		{
			pstmt = DB.prepareStatement(sql,null);
			rs = pstmt.executeQuery();	
					
			while(rs.next ()){
				X_C_Project pr = new X_C_Project(Env.getCtx(), rs, null);
				result.add(pr);
			}
		}
		catch (SQLException e)
		{
			CLogger.get().log(Level.INFO, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		
		X_C_Project[] retValue = new X_C_Project[result.size ()];
		
		result.toArray(retValue);
		
		return retValue;
	}

}
